package com.tongyi.lizhishop.activitys;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.allen.library.SuperTextView;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.BaseActivity;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.tongyi.lizhishop.managers.APPUpdateManager;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;

    @BindView(R.id.changePassword)
    SuperTextView changePassword;
    @BindView(R.id.changePhoneNumber)
    SuperTextView changePhoneNumber;
    @BindView(R.id.platformIntroduce)
    SuperTextView platformIntroduce;
    @BindView(R.id.adviceFeedback)
    SuperTextView adviceFeedback;
    @BindView(R.id.serviceNumber)
    SuperTextView serviceNumber;
    @BindView(R.id.messageNofi)
    SuperTextView messageNofi;
    @BindView(R.id.loginOut)
    SuperTextView loginOut;
    @BindView(R.id.activity_setting)
    LinearLayout activitySetting;
    @BindView(R.id.update)
    SuperTextView update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titlebar.getCenterTextView().setText("设置");
        titlebar.setListener(new CommonTitleBar.DefaultOnTitleBarListener(this));
        messageNofi.setSwitchIsChecked(SPUtils.getInstance().getBoolean("messageNofi", false));
        messageNofi.setSwitchCheckedChangeListener(new SuperTextView.OnSwitchCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                submiData(b);
            }
        });
    }

    private void submiData(final boolean result) {

        String submitDa = result ? "1" : "2";
        APIServiceManager.getInstance().create(MessageApi.class)
                .updateMessState(SPUtils.getInstance().getString("userid"), submitDa)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        LogUtils.w(value);
                        if ("succ".equals(value.getRe())) {
                            SPUtils.getInstance().put("messageNofi", result);
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        ToastUtils.showShort("请检查网络");
                    }
                });
    }

    @OnClick({R.id.changePassword, R.id.changePhoneNumber, R.id.platformIntroduce, R.id.adviceFeedback, R.id.serviceNumber, R.id.loginOut, R.id.activity_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.changePassword:
                ActivityUtils.startActivity(this, ChangePasswordActivity.class);
                break;
            case R.id.changePhoneNumber:
                ActivityUtils.startActivity(this, ChangePhoneNumberActivity.class);
                break;
            case R.id.platformIntroduce:
                ActivityUtils.startActivity(this, PlatformIntroduceActivity.class);
                break;
            case R.id.adviceFeedback:
                ActivityUtils.startActivity(this, FeedBackActivity.class);
                break;
            case R.id.serviceNumber:
                Intent dialIntent = IntentUtils.getDialIntent(serviceNumber.getRightTextView().getText().toString());
                startActivity(dialIntent);
                break;
            case R.id.messageNofi:
                ActivityUtils.startActivity(this, MessageNotifiSettingActivity.class);
                break;
            case R.id.loginOut:
                new AlertDialog.Builder(this)
                        .setTitle("确定退出")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                SPUtils.getInstance().clear();
                                ActivityUtils.startActivity(SettingActivity.this, LoginActivity.class);
                                LocalBroadcastManager.getInstance(SettingActivity.this).sendBroadcast(new Intent(BaseActivity.CLOSE_APP));
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

                break;

        }
    }

    @OnClick(R.id.update)
    public void onViewClicked() {

        APPUpdateManager appUpdateManager = new APPUpdateManager(this);

        appUpdateManager.checkVersion();

    }
}
